package com.adinnet.api.order.service.impl;

import com.adinnet.api.child.mapper.ChildMapper;
import com.adinnet.api.child.model.Child;
import com.adinnet.api.order.model.Order;
import com.adinnet.api.order.mapper.OrderMapper;
import com.adinnet.api.order.model.vo.OrderDetailsResp;
import com.adinnet.api.order.model.vo.OrderReq;
import com.adinnet.api.order.model.vo.OrderResp;
import com.adinnet.api.order.service.OrderService;
import com.adinnet.api.peaseRecord.mapper.PeaseRecordMapper;
import com.adinnet.api.peaseRecord.model.PeaseRecord;
import com.adinnet.common.utils.UUIDUtil;
import com.adinnet.core.JsonResult;
import com.adinnet.core.exception.BizException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Random;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-03
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ChildMapper childMapper;

    @Autowired
    private PeaseRecordMapper peaseRecordMapper;

    /**
     * 兑换
     *
     * @param orderReq
     * @param request
     * @return
     */
    @Override
    @Transactional
    public JsonResult createOrder(OrderReq orderReq, HttpServletRequest request) {
        Integer childId = orderReq.getChildId();
        Child child = childMapper.selectById(childId);
        if (null == child) {
            throw new BizException("儿童信息有误!");
        }
        //校验是否可以兑换
        int totalRattanNum = orderReq.getProductPrice() * orderReq.getProductNum();
        if (child.getRattanNum() < totalRattanNum) {
            throw new BizException("豆藤不足!");
        }
        Order order = new Order();
        BeanUtils.copyProperties(orderReq, order);
        order.setOrderNo(new Random().nextInt(8)+UUIDUtil.generateOrderNo());
        order.setStatus(0);
        order.setPrice(totalRattanNum);
        orderMapper.insert(order);
        child.setRattanNum(child.getRattanNum() - totalRattanNum);
        childMapper.updateById(child);
        savePeaseRecord(orderReq, totalRattanNum);
        return JsonResult.ok("兑换成功!");
    }

    /**
     * 保存豌豆记录
     *
     * @param orderReq
     * @param totalRattanNum
     */
    private void savePeaseRecord(OrderReq orderReq, int totalRattanNum) {
        PeaseRecord peaseRecord = new PeaseRecord();
        peaseRecord.setType(2);
        peaseRecord.setChildId(orderReq.getChildId());
        peaseRecord.setPeaseNum(-(totalRattanNum));
        peaseRecord.setSource(11);
        peaseRecord.setSourceName("兑换" + orderReq.getProductName());
        peaseRecord.setStatus(3);
        peaseRecordMapper.insert(peaseRecord);
    }

    /**
     *
     *订单列表
     * @param childId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Page<OrderResp> orderList(Integer childId, Integer page, Integer limit) {
        return childMapper.orderList(new Page<>(page,limit),childId);
    }

    /**
     * 订单详情
     * @param orderNo
     * @return
     */
    @Override
    public OrderDetailsResp orderDetails(String orderNo) {
        return childMapper.orderDetails(orderNo);
    }
}
